package com.bootdo.core.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.bootdo.common.utils.DateUtil;
import com.bootdo.core.dao.IncomeFlowMapper;
import com.bootdo.core.dao.UserCoreDao;
import com.bootdo.core.domain.IncomeFlow;
import com.bootdo.core.domain.Transaction;
import com.bootdo.core.domain.UserCoreDO;
import com.bootdo.core.service.IAccountService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.core.domain.ShortVideoComplaintDO;
import com.bootdo.core.service.ShortVideoComplaintService;

/**
 * 小视频举报表
 * 
 * @author xgc
 * @email 1992lcg@163.com
 * @date 2019-04-17 19:20:41
 */
 
@Controller
@RequestMapping("/core/shortVideoComplaint")
public class ShortVideoComplaintController {
	@Autowired
	private ShortVideoComplaintService shortVideoComplaintService;
	@Autowired
	UserCoreDao userCoreDao;
	@Autowired
	IAccountService iAccountService;
	@Autowired
	IncomeFlowMapper incomeFlowMapper;
	
	@GetMapping()
	@RequiresPermissions("core:shortVideoComplaint:shortVideoComplaint")
	String ShortVideoComplaint(){
	    return "core/shortVideoComplaint/shortVideoComplaint";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("core:shortVideoComplaint:shortVideoComplaint")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ShortVideoComplaintDO> shortVideoComplaintList = shortVideoComplaintService.list(query);
		int total = shortVideoComplaintService.count(query);
		PageUtils pageUtils = new PageUtils(shortVideoComplaintList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("core:shortVideoComplaint:add")
	String add(){
	    return "core/shortVideoComplaint/add";
	}

	@GetMapping("/edit/{id}")
//	@RequiresPermissions("core:shortVideoComplaint:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		ShortVideoComplaintDO shortVideoComplaint = shortVideoComplaintService.get(id);
		model.addAttribute("shortVideoComplaint", shortVideoComplaint);
	    return "core/shortVideoComplaint/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("core:shortVideoComplaint:add")
	public R save( ShortVideoComplaintDO shortVideoComplaint){
		if(shortVideoComplaintService.save(shortVideoComplaint)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
//	@RequiresPermissions("core:shortVideoComplaint:edit")
	public R update( ShortVideoComplaintDO shortVideoComplaint){
		shortVideoComplaint.setStatus(1);
		shortVideoComplaintService.update(shortVideoComplaint);
		//增加多币
		String orderId= UUID.randomUUID().toString();
		Transaction transaction=new Transaction();
		transaction.setUserId(shortVideoComplaint.getUserId().toString());
		transaction.setAccountType(1);
		transaction.setAmount(shortVideoComplaint.getAmount());
		transaction.setOrderId(orderId);
		iAccountService.increase(transaction);
		//增加积分
		UserCoreDO user=new UserCoreDO();
		user.setId(shortVideoComplaint.getUserId());
		user.setTotalPoint(shortVideoComplaint.getTotalPoint().intValue());
		userCoreDao.updatePoint(user);
		IncomeFlow incomeFlow=new IncomeFlow();
		incomeFlow.setUserId(shortVideoComplaint.getUserId().toString());
		incomeFlow.setType(1);
		incomeFlow.setAccountDate(DateUtil.format(new Date(),DateUtil.YYYYMMDD));
		incomeFlow.setContent(shortVideoComplaint.getTotalPoint().intValue()+"积分+"+shortVideoComplaint.getAmount()+"多币");
		incomeFlow.setOrderId(orderId);
		incomeFlowMapper.insertSelective(incomeFlow);
		return R.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/updateFail")
//	@RequiresPermissions("core:shortVideoComplaint:edit")
	public R updateFail( ShortVideoComplaintDO shortVideoComplaint){
		shortVideoComplaint.setStatus(2);
		shortVideoComplaintService.update(shortVideoComplaint);
		return R.ok();
	}
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("core:shortVideoComplaint:remove")
	public R remove( Integer id){
		if(shortVideoComplaintService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 审核
	 */
	@PostMapping( "/review")
	@ResponseBody
	@RequiresPermissions("core:shortVideoComplaint:review")
	public R review( Integer id){
		if(shortVideoComplaintService.review(id,"1")>0){
		return R.ok();
		}
		return R.error();
	}
	/**
	 * 审核失败
	 */
	@PostMapping( "/fail")
	@ResponseBody
	@RequiresPermissions("core:shortVideoComplaint:fail")
	public R fail(Integer id){
		if(shortVideoComplaintService.review(id,"2")>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("core:shortVideoComplaint:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		shortVideoComplaintService.batchRemove(ids);
		return R.ok();
	}
	
}
